package com.example.flowable.car;
import com.example.FlowableDb;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wsl
 */
public class FlowableProInst extends FlowableDb {

    public FlowableProInst(){
        super("car:1:d532aff5-e173-11ec-a19f-ac74b110b28d");
    }

    @Test
    public void startProcess() {
//        构建流程便量
        Map<String,Object> variables = new HashMap<>();
        variables.put("admSpecialist","3");//审批人
        variables.put("empName","");//申请人
        variables.put("licensePlate","");//车牌号
        variables.put("brand","");//车辆名称
        variables.put("kaishiTime",new Date());//开始时间
        variables.put("jieshuTime",new Date());//结束时间
        variables.put("cause","");//申请原由
        Authentication.setAuthenticatedUserId("3");//设置流程的发起人
        runtimeService
                .startProcessInstanceById(processDefinitionId,variables);
    }

    /**
     * 查看当前流程状态
     */
    @Test
    public void t() {
        List<ProcessInstance> list = runtimeService
                .createProcessInstanceQuery()
                .processDefinitionId(processDefinitionId)
                .startedBy("3")
                .includeProcessVariables()
                .list();
        for (ProcessInstance processInstance : list) {
            ActivityInstance activityInstance = runtimeService
                    .createActivityInstanceQuery()
                    .processInstanceId(processInstance.getId())
                    .unfinished()
                    .singleResult();

            System.out.println("流程：" + processInstance.getId());
            System.out.println("当前节点：" + activityInstance.getActivityName());
            System.out.println("负责人：" + activityInstance.getAssignee());
        }
    }
}
